package com.wangxiang.shop.member.controller;

import cn.hutool.core.util.RandomUtil;
import com.wangxiang.common.utils.PageEntity;
import com.wangxiang.common.utils.R;
import com.wangxiang.shop.member.entity.MemberEntity;
import com.wangxiang.shop.member.feign.ManageFeign;
import com.wangxiang.shop.member.feign.ThirdFeign;
import com.wangxiang.shop.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    ThirdFeign thirdFeign;
    @Autowired
    ManageFeign manageFeign;


    /**
     * 注册
     * @param
     * @return
     */
    @RequestMapping("register")
    public R register(@RequestBody HashMap<String , Object> map){
//        System.out.println(map);
        //表单校验
        String nullStr = "";
        //账号
        if (map.get("loginName")==null|nullStr.equals(map.get("loginName"))){
            return R.error("登录名为空");
        }
        //密码
        if (map.get("password")==null|nullStr.equals(map.get("password"))){
            return R.error("密码为空");
        }

        if (map.get("payPassword")==null|nullStr.equals(map.get("payPassword"))){
            return R.error("支付密码不能为空");
        }
        try {
            int payPassword = Integer.valueOf(map.get("payPassword").toString());
            String s = String.valueOf(payPassword);
            int bytes = s.getBytes().length;
            if (bytes<6){
                return R.error("支付密码不少于6位");
            }


        }catch (NumberFormatException n){
            return R.error("支付密码必须是数值");
        }
        MemberEntity memberEntity = memberService.selectUserByLoginName(map.get("loginName").toString());
        if (memberEntity!=null){
            return R.error("账号已被使用");
        }

        try {
            Integer.valueOf(map.get("age").toString());
        }catch (NumberFormatException n){
            return R.error("年龄必须是数值");
        }
        //验证码校验
        HashMap<String, Object> redisCaptcha = manageFeign.getRedisUniRegisterCaptcha();
//        System.out.println("redis"+redisCaptcha);
        //查看验证码是否超时，原理是查看uuid是否为空或者前端uuid和redis uuid不等
        if (redisCaptcha.get("uniRegisterUUID")==null|!map.get("uuid").equals(redisCaptcha.get("uniRegisterUUID"))){
            return R.error("验证码超时");
        }
        if(map.get("uuid").equals(redisCaptcha.get("uniRegisterUUID"))&!redisCaptcha.get("uniRegisterCaptcha").equals(map.get("captcha"))){
            return R.error("验证码错误,验证码时效5分钟，请再次查看邮箱");
        }

        //校验完毕，插入动作
        int register = memberService.register(map);
        if (register==0){
            return R.error("注册失败，未知原因");
        }

        return R.ok().put("msg","恭喜你成为商城会员");
    }
    /**
     * 邮箱验证码,注册
     */
    @RequestMapping("register/captcha")
    public R mailCaptcha(@RequestBody HashMap<String , Object> emial){
        String nullStr = "";
        System.out.println("邮箱"+emial);

        //邮箱校验
        if (emial.get("email")==null|nullStr.equals(emial.get("email"))){
            System.out.println("请输入邮箱");
            return R.error("请输入邮箱");
        }
        if (!emial.get("email").toString().contains(".com")){
            System.out.println("邮箱格式错误");
            return R.error("邮箱格式错误");
        }
        String uuid = RandomUtil.randomString(64);
        String captcha = RandomUtil.randomString(5);
        HashMap<String, String> map = new HashMap<>();
        map.put("uuid",uuid);
        map.put("captcha",captcha);
        int i = manageFeign.setRedisUniRegisterCaptcha(map);
        if (i==1){
            System.out.println("redis错误");
            return R.error("redis错误");
        }
        //redis存储成功后发送邮件
        HashMap<String, String> mailMap = new HashMap<>();
        mailMap.put("toEmail", emial.get("email").toString());
        mailMap.put("captcha",captcha);
        boolean mail = thirdFeign.mail(mailMap);
        if (!mail){
            System.out.println("发送邮件失败");
            return R.error("邮箱不存在");
        }

        return R.ok().put("uuid",uuid);
    }

    /**
     * 查询登录名账户是否已经存在
     * @param
     * @return
     */
    @RequestMapping("selectUser")
    public R selectUser(@RequestBody HashMap<String ,Object> map){

        String nullStr = "";
        if (map.get("loginName")==null|nullStr.equals(map.get("loginName"))){
            return R.error("登录名为空");
        }
        MemberEntity memberEntity = memberService.selectUserByLoginName(map.get("loginName").toString());
        if (memberEntity!=null){
            return R.error("账号："+map.get("loginName")+"已被使用");
        }

        return R.ok();
    }
    @RequestMapping("page")
    public R page(@RequestBody PageEntity page){
        PageEntity pageList = memberService.page(page);
        return R.ok().put("page",pageList);
    }

    @RequestMapping("search")
    public R serach(@RequestBody HashMap<String , Object> map){
        String nullStr = "";
       if (map.get("userName")==null){
           return R.error("请输入，再查询");
       }
       if (nullStr.equals(map.get("userName"))){
           return R.error("请输入，再查询");
       }
       //查询
        MemberEntity memberEntity = memberService.selectEntityByName(map.get("userName").toString());
       if (memberEntity==null){
           return R.error("用户不存在");
       }
        ArrayList<MemberEntity> list = new ArrayList<>();
       list.add(memberEntity);
       return R.ok().put("list",list);
    }

    /**
     * 失效用户需要填写理由
     * @param map
     * @return
     */
    @RequestMapping("invalid")
    public R invalid(@RequestBody HashMap<String , Object> map){
        String nullStr = "";
        if (map.get("remark")==null|nullStr.equals(map.get("remark"))){
            return R.error("失效失败，你未选择违规原因");
        }
        if (map.get("id")==null|nullStr.equals(map.get("id"))){
            return R.error("vue数据绑定错误，失效失败");
        }
        int i = memberService.updateStatus(map);
        if (i==0){
            return R.error("失效失败,未知原因");
        }
        return R.ok().put("msg","失效操作成功").put("code",800);

    }
    @RequestMapping("order/list")
    public List<HashMap<String , Object>> memberOrderList(@RequestBody List<HashMap<String , Object>> list){
        List<HashMap<String, Object>> orderList = memberService.memberOrderList(list);
        return orderList;
    }

    @RequestMapping("order/search")
    public int orderSearch(@RequestBody HashMap<String ,Object> map){
        //查询
        MemberEntity memberEntity = memberService.selectEntityByName(map.get("userName").toString());
        if (memberEntity==null){
            return 0;
        }else {
            return memberEntity.getUserId();
        }
    }

    @RequestMapping("line")
    public R line(@RequestBody Integer userId){
        memberService.line(userId,1);
        return R.ok();
    }


}
